package com.itmk.config.web.sys_role.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itmk.config.web.sys_role.entity.RoleParm;
import com.itmk.config.web.sys_role.entity.SelectType;
import com.itmk.config.web.sys_role.entity.SysRole;
import com.itmk.config.web.sys_role.service.SysRoleService;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping
    public ResultVO add(@RequestBody SysRole sysRole){
        sysRole.setCreateTime(new Date());
        if (sysRoleService.save(sysRole)){
            return ResultUtils.success("新增角色成功");
        }
        return ResultUtils.error("新增角色失败");
    }

    @PutMapping
    public ResultVO edit(@RequestBody SysRole sysRole){
        sysRole.setUpdateTime(new Date());
        if (sysRoleService.updateById(sysRole)){
            return ResultUtils.success("编辑角色成功");
        }
        return ResultUtils.error("编辑角色失败");
    }

    @DeleteMapping("/{roleId}")
    public ResultVO delete(@PathVariable("roleId") Long roleId){
        if (sysRoleService.removeById(roleId)){
            return ResultUtils.success("删除角色成功");
        }
        return ResultUtils.error("删除角色失败");
    }

    //查询
    public ResultVO list(RoleParm parm){
        IPage<SysRole> list = sysRoleService.getList(parm);
        return ResultUtils.success("查询成功",list);
    }

    //获取角色下拉数据
    @GetMapping("/getListSelect")
    public ResultVO getListSelect(){
        List<SysRole> list = sysRoleService.list();
        List<SelectType> roleList = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .stream()
                .forEach(item->{
                    SelectType type = new SelectType();
                    type.setValue(item.getRoleId());
                    type.setLabel(item.getRoleName());
                    roleList.add(type);
                });
        return ResultUtils.success("查询成功",roleList);
    }
}
